﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class CycleCountRequestBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly ICycleCountRequestDA CycleCountRequestDA = DataAccess.CreateCycleCountRequestDA();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;

        public CycleCountRequestBLL()
        {
            this.pubParasInfo = null;
        }

        public CycleCountRequestBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public CycleCountRequestBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteCycleCountRequest(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<CycleCountRequestInfo> CycleCountRequestInfos = new List<CycleCountRequestInfo>();
                CycleCountRequestInfo physicalRequestInfo = null;
                foreach (string id in IDS)
                {
                    physicalRequestInfo = CycleCountRequestDA.GetCycleCountRequestByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    CycleCountRequestInfos.Add(physicalRequestInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (CycleCountRequestDA.DeleteCycleCountRequest(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<CycleCountRequestInfo>(dataBase, tran, CycleCountRequestInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "PHYSICAL_REQUEST", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllCycleCountRequest()
        {
            return CycleCountRequestDA.GetAllCycleCountRequest(this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetCycleCountRequestByQueryEntity(CycleCountRequestQueryEntity RequestEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(RequestEntity.RequestID))
            {
                sqlWhere.Add(" REQUEST_ID " + RequestEntity.RequestID);
            }
            if (!string.IsNullOrEmpty(RequestEntity.Status))
            {
                sqlWhere.Add(" STATUS " + RequestEntity.Status);
            }
            if (!string.IsNullOrEmpty(RequestEntity.OwnerIdStart))
            {
                sqlWhere.Add("OWNER_ID_START " + RequestEntity.OwnerIdStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.OwnerIdEnd))
            {
                sqlWhere.Add(" OWNER_ID_END " + RequestEntity.OwnerIdEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.OwnerIdEnd))
            {
                sqlWhere.Add(" OWNER_ID_END " + RequestEntity.OwnerIdEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.SkuIdStart))
            {
                sqlWhere.Add(" SKU_ID_START " + RequestEntity.SkuIdStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.SkuIdEnd))
            {
                sqlWhere.Add(" SKU_ID_END " + RequestEntity.SkuIdEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.LocStart))
            {
                sqlWhere.Add(" LOC_START " + RequestEntity.LocStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.LocEnd))
            {
                sqlWhere.Add(" LOC_END " + RequestEntity.LocEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.LocLevelStart))
            {
                sqlWhere.Add(" LOC_LEVEL_START " + RequestEntity.LocLevelStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.LocLevelEnd))
            {
                sqlWhere.Add(" LOC_LEVEL_END " + RequestEntity.LocLevelEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.ZoneIdStart))
            {
                sqlWhere.Add(" ZONE_ID_START " + RequestEntity.ZoneIdStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.ZoneIdEnd))
            {
                sqlWhere.Add(" ZONE_ID_END " + RequestEntity.ZoneIdEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.AreaIdStart))
            {
                sqlWhere.Add(" AREA_ID_START " + RequestEntity.AreaIdStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.AreaIdEnd))
            {
                sqlWhere.Add(" AREA_ID_END " + RequestEntity.AreaIdEnd);
            }
            return CycleCountRequestDA.GetCycleCountRequestByQueryList(sqlWhere, RequestEntity, this.dataBaseID[0], this.pubParasInfo);
        }

        public CycleCountRequestInfo GetCycleCountRequestInfoByID(string id)
        {
            return CycleCountRequestDA.GetCycleCountRequestByID(id, this.dataBaseID[0], this.pubParasInfo);
        }

        public string InsertCycleCountRequest(CycleCountRequestInfo obj)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string PhysicalRequest = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(obj.RequestID))
                {
                    obj.RequestID = commonMethod.GenNextNumStr(dataBase, tran, "CYCLE_COUNT_REQUEST", whLoginID);
                }
                obj.UpdatedBy = obj.CreatedBy;
                obj.CreatedDate = DateTime.Now;
                obj.UpdatedDate = DateTime.Now;
                if (CycleCountRequestDA.InsertCycleCountRequest(dataBase, tran, obj, whLoginID) > 0)
                {
                    PhysicalRequest = obj.RequestID;
                    new AuditDataBLL().InsertAuditData<CycleCountRequestInfo>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "CYCLE_COUNT_REQUEST", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return PhysicalRequest;
        }

        public string UpdateCycleCountRequest(CycleCountRequestInfo obj)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string PhysicalRequest = string.Empty;
            try
            {
                CycleCountRequestInfo oldPhysicalRequestInfo = CycleCountRequestDA.GetCycleCountRequestByID(dataBase, tran, obj.RequestID, this.dataBaseID[0], this.pubParasInfo);
                if (CycleCountRequestDA.UpdateCycleCountRequest(dataBase, tran, obj, whLoginID) > 0)
                {
                    PhysicalRequest = obj.RequestID;
                    new AuditDataBLL().InsertAuditData<CycleCountRequestInfo>(oldPhysicalRequestInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "CYCLE_COUNT_REQUEST", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return PhysicalRequest;
        }
    }
}

